package com.wjf.resourceservice.controller;

import com.wjf.common.entity.Result;
import com.wjf.resourceservice.entity.Playlist;
import com.wjf.resourceservice.entity.Music;
import com.wjf.resourceservice.service.FileService;
import com.wjf.resourceservice.service.PlaylistService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/playlists")
public class PlaylistController {
    
    private static final Logger log = LoggerFactory.getLogger(PlaylistController.class);
    
    @Autowired
    private PlaylistService playlistService;

    @Autowired
    private FileService fileService;
    
    /**
     * 创建歌单
     */
    @PostMapping("/add")
    public Result<?> createPlaylist(@RequestParam(value = "cover", required = false) MultipartFile coverFile,
                                    @RequestParam String name,
                                    @RequestParam String description,
                                    @RequestParam Integer type,
                                    @RequestParam Integer status) {
        try {
            if (name == null || name.trim().isEmpty()) {
                return Result.error("歌单名称不能为空");
            }

            // 上传封面（如果有）
            String coverUrl = null;
            if (coverFile != null) {
                coverUrl = fileService.uploadImage(coverFile);
            }
            Playlist playlist = new Playlist();
            playlist.setName(name);
            playlist.setDescription(description);
            playlist.setType(type);
            playlist.setStatus(status);
            playlist.setCoverUrl(coverUrl);
            boolean success = playlistService.createPlaylist(playlist);
            if (success) {
                return Result.success("创建成功");
            } else {
                return Result.error("创建失败");
            }
        } catch (Exception e) {
            log.error("创建歌单失败", e);
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 添加音乐到歌单
     */
    @PostMapping("/{playlistId}/music/{musicId}")
    public Result<?> addMusicToPlaylist(
            @PathVariable Long playlistId,
            @PathVariable Long musicId) {
        try {
            boolean success = playlistService.addMusicToPlaylist(playlistId, musicId);
            if (success) {
                return Result.success("添加成功");
            } else {
                return Result.error("添加失败");
            }
        } catch (Exception e) {
            log.error("添加音乐到歌单失败", e);
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取用户的歌单列表
     */
    @GetMapping("/user/{userId}")
    public Result<?> getUserPlaylists(@PathVariable Long userId) {
        try {
            List<Playlist> playlists = playlistService.getUserPlaylists(userId);
            return Result.success(playlists);
        } catch (Exception e) {
            log.error("获取用户歌单列表失败", e);
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取歌单中的音乐列表
     */
    @GetMapping("/{playlistId}/music")
    public Result<?> getPlaylistMusics(@PathVariable Long playlistId) {
        try {
            List<Music> musicList = playlistService.getPlaylistMusics(playlistId);
            return Result.success(musicList);
        } catch (Exception e) {
            log.error("获取歌单音乐列表失败", e);
            return Result.error(e.getMessage());
        }
    }
} 